# 编译器和标志
CXX := g++
CXXFLAGS := -std=c++17 -pthread -Wall -Wextra -g -O2
DEPFLAGS := -MMD -MP

# 包含路径
INCLUDES = \
    -I. \
    -Ilog4cpp/include \
    -I/usr/include/mysql \
    -I/usr/include/mariadb \
	-I/usr/include/cppconn   \
	-I/usr/include/cjson# 新增关键路径


# 库路径和链接
LDFLAGS := \
    -L. \
    -Llog4cpp/src \
    -L/usr/lib/mysql

LIBS := \
    -llog4cpp \
    -lmysqlclient \
    -lmysqlcppconn \
    -lpthread \
	-lcrypto \
	-lcurl \
	-lavformat \
	-lavcodec \
	-lavutil \
	-lswscale

# 源文件 (基于您提供的列表)
CPP_SOURCES := \
    Acceptor.cpp \
    BussinessService.cpp \
    Condition.cpp \
    Configuration.cpp \
    DbConnectionPool.cpp \
    EventLoop.cpp \
    InetAddress.cpp \
    MutexLock.cpp \
    Mylogger.cpp \
    SmartHomeMonitorServer.cpp \
    Socket.cpp \
    SocketIO.cpp \
    TaskQueue.cpp \
    TcpConnection.cpp \
    Thread.cpp \
    ThreadPool.cpp \
    test.cpp \
	GenerateToken.cpp

# 生成对应的目标文件列表
OBJECTS := $(CPP_SOURCES:.cpp=.o)
DEPENDS := $(CPP_SOURCES:.cpp=.d)

# 最终目标
TARGET := SmartHomeMonitorServer

# 默认目标
all: $(TARGET)

# 链接可执行文件
$(TARGET): $(OBJECTS)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS)
	@echo "\n\033[1;32m[✓] 编译成功: $@\033[0m"

# 自动生成依赖关系 + 编译规则
%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@

# 包含自动生成的依赖文件
-include $(DEPENDS)

# 清理
clean:
	rm -f $(TARGET) $(OBJECTS) $(DEPENDS)
	@echo "\033[1;33m[!] 已清理构建文件\033[0m"

rebuild:clean all

# 安装依赖提示
install-deps:
	@echo "\033[1;34m[>] 正在安装依赖...\033[0m"
	sudo apt-get install -y libmysqlclient-dev libmysqlcppconn-dev g++
	@echo "\033[1;32m[✓] 依赖安装完成\033[0m"

# 设置开发环境
setup: install-deps
	cd log4cpp && ./configure && make
	@echo "\033[1;32m[✓] 环境准备就绪！执行 'make' 开始构建\033[0m"

# 调试信息
info:
	@echo "\033[1;36m[项目信息]\033[0m"
	@echo "目标文件: $(TARGET)"
	@echo "源文件: $(CPP_SOURCES)"
	@echo "包含路径:"
	@echo "  $(subst -I, ,$(INCLUDES))" | tr ' ' '\n' | sed 's/^/  • /'
	@echo "链接库:"
	@echo "  $(subst -l, ,$(LIBS))" | tr ' ' '\n' | sed 's/^/  • /'

.PHONY: all clean install-deps setup info
